﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Mail;

public partial class car_insurance_callme : MyLanguageBasePage
{
    CE.User.Profile _profile = null;
    CE.User.ProfileDB _profiledb = null;
    Contact _contact = null;
    ContactDB _contactdb = null;
    string _redirectUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlLink css = new HtmlLink();
        css.Href = Resources.General.CssUrl;
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";
        Page.Header.Controls.Add(css);

        HtmlLink cssButton = new HtmlLink();
        cssButton.Href = Resources.General.CssButtonUrl;
        cssButton.Attributes["rel"] = "stylesheet";
        cssButton.Attributes["type"] = "text/css";
        cssButton.Attributes["media"] = "all";
        Page.Header.Controls.Add(cssButton);
        Page.Header.Controls.Add(css);

        if (!Page.IsPostBack)
        {
            txtEmailAddress.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmailAddress.ClientID + "','" + rfvEmail.ClientID + "','" + rfvEmailValid.ClientID + "');");
            txtCallMeFullName.Attributes.Add("onblur", "ShowValidationEffect('" + txtCallMeFullName.ClientID + "','" + rfvCallMeFullName.ClientID + "');");
            ddlCallMeTime.Attributes.Add("onblur", "ShowValidationEffect('" + ddlCallMeTime.ClientID + "','" + rfvCallMeTime.ClientID + "');");

            if (Session[General.UserId] != null)
            {
                rdoExistingYes.Checked = true;
                ShowCustomerDetail(Convert.ToInt64(Session[General.UserId]));
            }
        }
    }

    private void ChangeRequiredFieldValidation(WebControl ctl, RequiredFieldValidator rfv, string strValidation)
    {
        if (rfv.Enabled && rfv.ValidationGroup == strValidation)
        {
            rfv.Validate();
            if (rfv.IsValid == false)
            {
                ctl.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ctl.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ctl.ControlStyle.CssClass = "";
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        #region "Call Me"
        //FullName
        ChangeRequiredFieldValidation(txtCallMeFullName, rfvCallMeFullName, strValidationGroup);

        if (revCallMeFullName.Enabled && revCallMeFullName.ValidationGroup == strValidationGroup)
        {
            if (rfvCallMeFullName.IsValid)
            {
                revCallMeFullName.Validate();
                if (revCallMeFullName.IsValid == false)
                {
                    txtCallMeFullName.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtCallMeFullName.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtCallMeFullName.ControlStyle.CssClass = "";
        }
        //Email
        ChangeRequiredFieldValidation(txtEmailAddress, rfvEmail, strValidationGroup);
        //Time
        ChangeRequiredFieldValidation(ddlCallMeTime, rfvCallMeTime, strValidationGroup);

        //Mobile Number
        usrCallMePhoneNo.ChangeValidationEffectServerSide(strValidationGroup);
        #endregion
    }

    private void ShowCustomerDetail(Int64 UserId)
    {
        _profile = new CE.User.Profile();
        _profiledb = new ProfileDB();
        _contact = new Contact();
        _contactdb = new ContactDB();

        _profile = _profiledb.GetProfile(UserId);
        _profiledb = null;

        if (_profile != null)
        {
            //ddlSalutation.Saluation = _profile.Salutation;
            txtCallMeFullName.Text = _profile.FirstName + " " + _profile.SurName;


            _contact = _contactdb.GetContact(UserId);
            if (_contact != null)
            {
                usrCallMePhoneNo.PhoneNo = _contact.Mobile;
                txtEmailAddress.Text = _contact.Email;
                txtEmailAddress.ReadOnly = true;

            }

            _profile = null;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (usrCallMePhoneNo.GetPhoneNo().Length == 10)
        {
            string _message = "";
            string _title = "";

            txtCallMeFullName.Text = General.ClearScriptTag(txtCallMeFullName.Text);
            _message = "Customer Name: " + Server.HtmlEncode(txtCallMeFullName.Text);
            _title = "Call Me Now - Call Me request by " + Server.HtmlEncode(txtCallMeFullName.Text);
            ChangeValidationEffectServerSide("callme");
            if (IsValid)
            {
                Int64 _result = 0;
                ContactUser _cu = new ContactUser();
                ContactUserDB _cudb = new ContactUserDB();

                string strInsType = Request.Params["instype"].ToString();
                if (strInsType == "10")
                {
                    strInsType = "1";
                }
                else if (strInsType == "11")
                {
                    strInsType = "1+";
                }
                else if (strInsType == "21")
                {
                    strInsType = "2+";
                }
                else if (strInsType == "30")
                {
                    strInsType = "3+";
                }
                else if (strInsType == "31")
                {
                    strInsType = "3+";
                }

                _cu.InsuranceType = "CI-" + strInsType;
                _cu.ProductId = Convert.ToInt64(Request.Params["carid"].ToString());
                _cu.RefCEUserId = Convert.ToInt64(Request.Params["uid"].ToString());

                _cu.ProviderCompanyId = Convert.ToInt64(Request.Params["pcid"].ToString());
                _cu.ContactNo = usrCallMePhoneNo.GetPhoneNo();
                _cu.FullName = Server.HtmlEncode(txtCallMeFullName.Text);
                _cu.CallTime = Convert.ToInt16(ddlCallMeTime.Text);

                if (Request.Params["spc"] != null)
                {
                    if (Request.Params["spc"].ToString() == "1")
                    {
                        _cu.Info1 = "GG Promotion";
                    }
                }

                _result = _cudb.Insert(_cu);

                if (_result > 0)
                {
                    _profile = new CE.User.Profile();
                    _profiledb = new ProfileDB();
                    _profile = _profiledb.GetProfile(_cu.RefCEUserId);
                    if (_profile != null && _profile.ProfileId > 0)
                    {
                        _profile.SurName = string.Empty;
                        _profile.FirstName = Server.HtmlEncode(txtCallMeFullName.Text);
                        _profiledb.Insert(_profile);
                    }
                    _profile = null;
                    _profiledb = null;

                    _contact = new Contact();
                    _contactdb = new ContactDB();
                    _contact = _contactdb.GetContact(_cu.RefCEUserId);
                    if (_contact != null && _contact.ContactId > 0)
                    {
                        _contact.Mobile = usrCallMePhoneNo.GetPhoneNo();
                        _contactdb.Insert(_contact);
                    }
                    _contact = null;
                    _contactdb = null;

                    CE.CarInsurance.CarDetailTH _cardetail = new CE.CarInsurance.CarDetailTH();
                    CE.CarInsurance.CarDetailTHDB _cardetailDB = new CE.CarInsurance.CarDetailTHDB();
                    _cardetail = _cardetailDB.GetCar(_cu.ProductId, _cu.RefCEUserId);
                    if (_cardetail != null && _cardetail.CarId > 0)
                    {
                        _cardetail.Info7 = string.Empty;
                        _cardetail.Info8 = Server.HtmlEncode(txtCallMeFullName.Text);
                        _cardetailDB.Insert(_cardetail);
                    }
                    _cardetail = null;
                    _cardetailDB = null;

                    _message += "<img src='http://www.gluaygluay.com/images/callme/callme-home.png' /><br/>";
                    _message += "<br/><br/>Insurance Type : " + strInsType;
                    _message += "<br/><br/>Full Name : " + Server.HtmlEncode(txtCallMeFullName.Text);
                    _message += "<br/><br/>Email : " + txtEmailAddress.Text;
                    _message += "<br/><br/>Contact No : " + usrCallMePhoneNo.GetPhoneNo();
                    _message += "<br/><br/>Call Time : " + ddlCallMeTime.SelectedItem.Text;
                    string _emailList = ConfigurationManager.AppSettings["TH.CICallMeMessageReceiver"].ToString();
                    //MyMailManager.SendCRMMessage(_emailList, _title, _message);

                    divCallMe.Visible = true;
                    lblCallMeMessage.Text = Resources.CarInsurance.CallMeMessage;

                    txtCallMeFullName.ReadOnly = true;
                    txtEmailAddress.ReadOnly = true;
                    ddlCallMeTime.Enabled = false;
                    usrCallMePhoneNo.IsEnabled = false;
                    btnSubmit.Enabled = false;
                }
            }
        }

    }

}
